<?php

namespace App\Admin\Metrics;

use App\Models\Bonu;
use App\Models\ConfigModel;
use App\Models\ProductModel;
use App\Models\ServiceIssue;
use Carbon\Carbon;
use Closure;
use Dcat\Admin\Grid\LazyRenderable as LazyGrid;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Card;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Redis;

class BannerSqIssueCounts extends Card
{
    /**
     * @param string|Closure|Renderable|LazyWidget $content
     *
     * @return BannerServiceIssueCounts
     */
    public function content($content): BannerSqIssueCounts
    {
        if ($content instanceof LazyGrid) {
            $content->simple();
        }
        //TASK_START 开始时间
        $val = ConfigModel::query()->where('key','TASK_START')->value('val');
        $config_task_start = $val ? $val :'2022-1-1';
        $task_start = Carbon::parse($config_task_start);
        $task_start_time = Carbon::parse($task_start);

        $now = Carbon::parse(Carbon::now()->format('Y-m-d'));
        if($now->gt($task_start_time)){
            $now_cycle = $now->diffInMonths($task_start_time);
            $now_cycle += 1;
        }else{
            $now_cycle = 1;
        }
        $now_cycle = $now_cycle -1;
        //连接redis
        $redis =  Redis::connection('default');
        $bonus = Bonu::query()->where('cycle',$now_cycle)->first();
        $bonus_now = isset($bonus) ? ($bonus->amount + $bonus->amount_extra)/100 : 0;
        $bonus_pre = $redis->zScore('prize_pool_pre',$now_cycle)/100;  //预估奖金池
        $text = trans('上期预计分红/总分红');


//        $route = admin_route('service.issues.index');
        $html = <<<HTML
<a href="">
    <div class="small-box" style="padding:0 20px;height:100px;margin-bottom: 0;border-radius: .25rem;background: url('static/images/yellow.png') no-repeat;background-size: 100% 100%;">
    <div class="inner">
        <h4 style="color: white;font-size: 30px;text-shadow: #888888 1px 1px 2px;">{$bonus_pre}/{$bonus_now}</h4>
        <p style="color: white;text-shadow: #888888 1px 1px 2px;">{$text}</p>
    </div>
</div>
</a>
HTML;

        $this->content = $this->formatRenderable($html);
        $this->noPadding();

        return $this;
    }
}
